package org.dtrd.modules.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.common.system.vo.LoginUser;
import org.dtrd.modules.service.entity.bean.ServiceCategoryInfo;
import org.dtrd.modules.service.entity.po.DtrdEntRdService;
import org.dtrd.modules.service.entity.po.DtrdEntRdServiceCategory;
import org.dtrd.modules.service.mapper.DtrdEntRdServiceCategoryMapper;
import org.dtrd.modules.service.service.IDtrdEntRdServiceCategoryService;
import org.dtrd.modules.service.service.IDtrdEntRdServiceService;
import org.dtrd.modules.util.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务套餐分类信息 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-11-19 18:52:54
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdServiceCategoryServiceImpl extends ServiceImpl<DtrdEntRdServiceCategoryMapper, DtrdEntRdServiceCategory> implements IDtrdEntRdServiceCategoryService {

    @Autowired
    private IDtrdEntRdServiceService serviceService;

    @Override
    public List<ServiceCategoryInfo> getServiceCategoryList() {
        LambdaQueryWrapper<DtrdEntRdServiceCategory> queryWrapper = baseQueryWrapper();
        LoginUser loginUser = ShiroUtil.getLoginUser();
        if (loginUser != null) {
            queryWrapper.eq(DtrdEntRdServiceCategory::getOrgCode, loginUser.getOrgCode());
        }
        List<DtrdEntRdServiceCategory> poList = this.list(queryWrapper);
        List<ServiceCategoryInfo> dtoList = new ArrayList<>(poList.size());
        if (CollectionUtil.isNotEmpty(poList)){
            dtoList = poList.stream()
                    .map(po -> new ServiceCategoryInfo().parseFromPo(po))
                    .collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public List<ServiceCategoryInfo> getServiceCategoryList(String orgCode) {
        LambdaQueryWrapper<DtrdEntRdServiceCategory> queryWrapper = baseQueryWrapper()
                .eq(StringUtils.isNotBlank(orgCode),
                        DtrdEntRdServiceCategory::getOrgCode, orgCode);
        List<DtrdEntRdServiceCategory> poList = this.list(queryWrapper);
        List<ServiceCategoryInfo> dtoList = new ArrayList<>(poList.size());
        if (CollectionUtil.isNotEmpty(poList)){
            dtoList = poList.stream()
                    .map(po -> new ServiceCategoryInfo().parseFromPo(po))
                    .collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public boolean saveOrUpdateServiceCategoryInfo(ServiceCategoryInfo dto) {
        DtrdEntRdServiceCategory serviceCategory = new DtrdEntRdServiceCategory();
        LoginUser loginUser = ShiroUtil.getLoginUser();
        if (dto.getDataId() == null) {
            serviceCategory = serviceCategory.init();
            // 保存操作者的 id 和 orgCode
            if (loginUser != null) {
                serviceCategory.setOrgCode(loginUser.getOrgCode());
                serviceCategory.setCreateBy(loginUser.getId());
            }
        }else {
            // 设置修改者的 id
            if (loginUser != null) {
                serviceCategory.setUpdateBy(loginUser.getId());;
            }
        }
        serviceCategory = serviceCategory.parseFromDto(dto);
        return saveOrUpdate(serviceCategory);
    }

    @Override
    public boolean hasSameServiceCategoryName(Integer categoryId, String categoryName, String orgCode) {
        LambdaQueryWrapper<DtrdEntRdServiceCategory> wrapper = baseQueryWrapper()
                .eq(DtrdEntRdServiceCategory::getOrgCode, orgCode)
                .eq(DtrdEntRdServiceCategory::getCategoryName, categoryName);
        DtrdEntRdServiceCategory category = getOne(wrapper);
        return category != null && !Objects.equals(category.getDataId(), categoryId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeServiceCategoryInfo(Integer categoryId) {
        boolean result = this.removeById(categoryId);
        LambdaQueryWrapper<DtrdEntRdService> removeWrapper = Wrappers.lambdaQuery(DtrdEntRdService.class)
                .eq(DtrdEntRdService::getServiceCategoryId, categoryId);
        serviceService.remove(removeWrapper);
        return result;
    }

    @Override
    public ServiceCategoryInfo getServiceCategory(Integer categoryId) {
        LambdaQueryWrapper<DtrdEntRdServiceCategory> queryWrapper = baseQueryWrapper()
                .eq(DtrdEntRdServiceCategory::getDataId, categoryId);
        DtrdEntRdServiceCategory serviceCategoryPo = this.getOne(queryWrapper);
        return Optional.ofNullable(serviceCategoryPo)
                .map(po -> new ServiceCategoryInfo().parseFromPo(po))
                .orElse(null);
    }

    private LambdaQueryWrapper<DtrdEntRdServiceCategory> baseQueryWrapper(){
        return Wrappers.lambdaQuery(DtrdEntRdServiceCategory.class).eq(DtrdEntRdServiceCategory::getIsDel, 0);
    }
}
